//
//  TiyuCollectionViewCell.m
//  news
//
//  Created by imacbookair on 16/3/12.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "TiyuCollectionViewCell.h"
#import "DadaTableViewCell.h"
#import "DadaAnotherTableViewCell.h"
@interface TiyuCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>


@end

@implementation TiyuCollectionViewCell

#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_tableView];
        [_tableView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

#pragma mark - 布局

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 155 * KWITH, 0);
    
    // 重用池
    [_tableView registerClass:[DadaTableViewCell class] forCellReuseIdentifier:@"DadaTableViewCell"];
    [_tableView registerClass:[DadaAnotherTableViewCell class] forCellReuseIdentifier:@"DadaAnotherTableViewCell"];
    
    // 刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    _tableView.mj_header = header;
    
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    self.tableView.mj_footer = footer;
}

#pragma mark - 刷新方法
- (void)headerData
{
    NSString *urlStr = @"http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html";
    [AFNetBlock GetDataWithURL:urlStr block:^(id block) {
        self.dataArr = [TiyuModel modelHanderWithArray:block[@"T1348649079062"]];
        [_tableView reloadData];
    }];
}

- (void)footerData
{
    // 全局变量
    static NSInteger t = 40;
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348649079062/%ld-20.html", t];
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [TiyuModel modelHanderWithArray:block[@"T1348649079062"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    t += 20;
}
#pragma mark - 协议方法

- (UITableView *)tableViewSetWithUrlString: (NSString *)urlStr
{
    [AFNetBlock GetDataWithURL:urlStr block:^(id block) {
        self.dataArr = [TiyuModel modelHanderWithArray:block[@"T1348649079062"]];
        [_tableView reloadData];
    }];
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TiyuModel *t = _dataArr[indexPath.row];
    if (indexPath.row == 0) {
        DadaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DadaTableViewCell" forIndexPath:indexPath];
        [cell.daTu sd_setImageWithURL:[NSURL URLWithString:t.imgsrc]zhanweitu];
        cell.biaoTi.text = t.title;
        [cell NightWithType:UIViewColorTypeNormal];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
        DadaAnotherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DadaAnotherTableViewCell" forIndexPath:indexPath];
        
        [cell.tu sd_setImageWithURL:[NSURL URLWithString:t.imgsrc]zhanweitu];
        
        cell.title.text = t.title;
        
        cell.neirong.text = t.digest;
        
        cell.pin.text = [NSString stringWithFormat:@"%@ 跟帖", t.replyCount.stringValue];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell NightWithType:UIViewColorTypeNormal];
        
        return cell;
    }
}

#pragma mark - 设置高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 180 * KHIGHT;
    }else{
        return 90 * KHIGHT;
    }
}

#pragma mark - 协议实现方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TiyuModel *t = _dataArr[indexPath.row];
    [self.delegate DadaValue:t.url];
}
@end
